#include "head.h"
int makeWorker(workerdata_t *workerdataArr, int workerNum){
    // 创建若干个子进程，子进程的信息保存在workerdataArr中
    pid_t pid;
    int pipefds[2];//父子进程要存在一个socketpair
    for(int i = 0; i < workerNum; ++i){
        socketpair(AF_LOCAL,SOCK_STREAM,0,pipefds);
        pid = fork();// fork返回非0，是子进程的pid
        if(pid == 0){
            // 让这个if永远走不完
            close(pipefds[0]);
            eventLoop(pipefds[1]);
        }
        // 父进程的逻辑
        close(pipefds[1]);
        workerdataArr[i].pid = pid;//存储子进程的pid
        workerdataArr[i].status = FREE;//子进程一启动是空闲的
        workerdataArr[i].pipefd = pipefds[0];//父进程保存每个子进程的管道
        printf("worker %d, pid = %d, pipefd = %d\n",i, pid, pipefds[0]);
    }
    return 0;
}
int eventLoop(int pipefd){
    while(1){
        int netfd;
        recvfd(pipefd,&netfd);//接收任务
        //sleep(10);//任务
        transFile(netfd);//子进程的任务打开文件并发送
        close(netfd);
        // 任务完成
        pid_t pid = getpid();
        write(pipefd,&pid,sizeof(pid));
    }
    return 0;
}
